package com.tudouji.project.harvest.controller;

import com.alibaba.fastjson.JSON;
import com.mchange.lang.IntegerUtils;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.MD5Utils;
import com.tudouji.common.utils.sign.Md5Utils;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.harvest.domain.Freightorder;
import com.tudouji.project.harvest.domain.WeightDeniedData;
import com.tudouji.project.harvest.domain.WeightSystemData;
import com.tudouji.project.harvest.mapper.FreightorderMapper;
import com.tudouji.project.harvest.service.IWeightDeniedDataService;
import com.tudouji.project.harvest.service.IWeightSystemDataService;
import com.tudouji.project.store.domain.Stockunit;
import com.tudouji.project.store.service.IStockunitService;
import com.tudouji.project.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.protocol.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/weight")
public class WeightedControoler extends BaseController {

    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IWeightSystemDataService systemDataService;
    @Autowired
    private IWeightDeniedDataService deniedDataService;
    @Autowired
    private IStockunitService stockunitService;

    @RequestMapping("/send")
    public AjaxResult saveWeightInfo(HttpServletRequest request) throws UnsupportedEncodingException {
        StringBuffer url = request.getRequestURL();
        String code = request.getParameter("code");
        String carno = request.getParameter("carno");
        String netweight = request.getParameter("netweight");
        String rawweight = request.getParameter("rawweight");
        String tareweight = request.getParameter("tareweight");
        String createtime = request.getParameter("createtime");
        String checksum = request.getParameter("checksum");

        System.out.println("-----------url------"+url.toString());
        System.out.println("-----------code------"+code);
        System.out.println("-----------carno------"+carno);
        System.out.println("-----------netweight------"+netweight);
        System.out.println("-----------rawweight------"+rawweight);
        System.out.println("-----------tareweight------"+tareweight);
        System.out.println("-----------createtime------"+createtime);
        System.out.println("-----------checksum------"+checksum);
        System.out.println();

        String token = sysConfigService.selectConfigByKey("weight_token");
        System.out.println("-----------token:------"+token);
        String enCodeStr = code /*+ carno*/ + rawweight + netweight + tareweight + createtime + token;
        String checksumCopy = Md5Utils.hash(enCodeStr);
        System.out.println("-----------MD5 String:------"+enCodeStr);
        System.out.println("-----------server:------"+checksumCopy);
        System.out.println("-----------consumer:------"+checksum);

        //验证通过-判断checksum
        if (StringUtils.equalsIgnoreCase(checksumCopy, checksum)){
            System.out.println("-----------compareResult:------"+StringUtils.equalsIgnoreCase(checksumCopy, checksum));

            WeightSystemData systemData = new WeightSystemData();
            systemData.setCode(code);
            systemData = systemDataService.selectWeightSystemDataLast(systemData);
            //第一次写入 是：直接写入system_data
            if (systemData == null){
                systemData = new WeightSystemData();
                saveWeightSD(code, carno, netweight, rawweight, tareweight, checksum, systemData);

            }else{
                if (systemData.getStatus() == 2 || systemData.getStatus() == 1){
                    systemData.setStatus(3);
                    systemDataService.updateWeightSystemData(systemData);
                }
                if (systemData.getStatus() == 2){
                    saveWeightDD(code, carno, netweight, rawweight, tareweight, checksum);
                }else{
                    if (!StringUtils.equalsIgnoreCase(systemData.getChecksum(), checksum)){
                        saveWeightSD(code, carno, netweight, rawweight, tareweight, checksum, systemData);
                    }
                }
            }
            return AjaxResult.success("true");
        }
        return AjaxResult.error("false");
    }

    private void saveWeightDD(String code, String carno, String netweight, String rawweight, String tareweight, String checksum) {
        WeightDeniedData deniedData = new WeightDeniedData();
        deniedData.setCode(code);
        deniedData.setCarno(carno);
        deniedData.setRawWeight(new BigDecimal(rawweight));
        deniedData.setTareWeight(new BigDecimal(tareweight));
        deniedData.setNetWeight(new BigDecimal(netweight));
        Stockunit stockunit = new Stockunit();
        stockunit.setIsUnit(1);
        stockunit.setToT(BigDecimal.ONE);
        List<Stockunit> stockunits = stockunitService.selectStockunitList(stockunit);
        deniedData.setWeightUnit(Integer.valueOf(stockunits.get(0).getId().toString()));
        deniedData.setWeightUnitName(stockunits.get(0).getUnitName());
        deniedData.setStatus(1);
        deniedData.setChecksum(checksum);
        deniedData.setCreateTime(DateUtils.getNowDate());
        deniedDataService.insertWeightDeniedData(deniedData);
    }

    private void saveWeightSD(String code, String carno, String netweight, String rawweight, String tareweight, String checksum, WeightSystemData systemData) {
        systemData.setCode(code);
        systemData.setCarno(carno);
        systemData.setRawWeight(new BigDecimal(rawweight));
        systemData.setTareWeight(new BigDecimal(tareweight));
        systemData.setNetWeight(new BigDecimal(netweight));
        Stockunit stockunit = new Stockunit();
        stockunit.setIsUnit(1);
        stockunit.setToT(BigDecimal.ONE);
        List<Stockunit> stockunits = stockunitService.selectStockunitList(stockunit);
        systemData.setWeightUnit(Integer.valueOf(stockunits.get(0).getId().toString()));
        systemData.setWeightUnitName(stockunits.get(0).getUnitName());
        systemData.setStatus(1);
        systemData.setChecksum(checksum);
        systemData.setCreateTime(DateUtils.getNowDate());
        systemDataService.insertWeightSystemData(systemData);
    }
}
